//
//  ColorVC.m
//  UILbrary
//
//  Created by Apple on 2022/5/14.
//  Copyright © 2022 mac. All rights reserved.
//

#import "ColorVC.h"
#import "CALayer+Tool.h"
#import "UIColor+Gradient.h"

@interface ColorVC ()

@end

@implementation ColorVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self configExplain:@"只做了线性渐变， 支持 横向渐变，纵向渐变，对角线渐变  推荐使用 UIColor+Gradient"];
    [self color_demo_1];
}

- (void)color_demo_1
{
    UIView *view = [UIView new];
    view.frame = CGRectMake(100, 300, 200, 200);
    [self.view addSubview:view];
    
    UIColor *color = [UIColor gradientColorWithSize:view.frame.size
                                          direction:GradientColorDirectionLevel
                                         startColor:[UIColor redColor]
                                           endColor:[UIColor orangeColor]];
    view.backgroundColor = color;
}

- (void)color_demo_2
{
    NSArray *colors = @[[UIColor greenColor],[UIColor redColor]];
    CGRect rect = CGRectMake(100, 300, 100, 100);
    CALayer *layer = [CALayer gradualLayerWithColors:colors frame:rect type:CALayerGradientColorType_TopToBottom];
    layer = [CALayer gradualLayerWithColors:colors vierticalOrHorizontal:NO frame:rect];

    
    [self.view.layer addSublayer:layer];
}

- (void)color_demo_3
{
    CGRect rect = CGRectMake(50, 300, 300, 200);

    UIColor *startColor = [UIColor greenColor];
    UIColor *endColor = [UIColor redColor];
    UIImage *gradualImage = [self gradualColorImage:rect colorArr:@[startColor,endColor]];
    
    UIImageView *imgView = [[UIImageView alloc] initWithImage:gradualImage];

    [self.view addSubview:imgView];
}

- (UIImage *)gradualColorImage:(CGRect )rect colorArr:(NSArray <UIColor *>*)colorArr
{
    //创建CGContextRef
    UIGraphicsBeginImageContext(self.view.bounds.size);
    CGContextRef gc = UIGraphicsGetCurrentContext();
    
    //创建CGMutablePathRef
    CGMutablePathRef path = CGPathCreateMutable();
    
    //绘制Path
    CGPathMoveToPoint(path, NULL, CGRectGetMinX(rect), CGRectGetMinY(rect));
    CGPathAddLineToPoint(path, NULL, CGRectGetMinX(rect), CGRectGetMaxY(rect));
    CGPathAddLineToPoint(path, NULL, CGRectGetWidth(rect), CGRectGetMaxY(rect));
    CGPathAddLineToPoint(path, NULL, CGRectGetWidth(rect), CGRectGetMinY(rect));

    CGPathCloseSubpath(path);
        
    // 绘制渐变
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGFloat locations[] = { 0.0, 1.0 };
    
    NSMutableArray *colors = [NSMutableArray array];
    [colorArr enumerateObjectsUsingBlock:^(UIColor * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [colors addObject:(__bridge id)obj.CGColor];
    }];

    CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef) colors, locations);
    CGRect pathRect = CGPathGetBoundingBox(path);
    
    //具体方向可根据需求修改
    CGPoint startPoint = pathRect.origin;//CGPointMake(CGRectGetMinX(pathRect), CGRectGetMinY(pathRect));
    CGPoint endPoint = CGPointMake(CGRectGetMaxX(pathRect), CGRectGetMaxY(pathRect));
    
    CGContextSaveGState(gc);
    CGContextAddPath(gc, path);
    CGContextClip(gc);
    CGContextDrawLinearGradient(gc, gradient, startPoint, endPoint, 0);
    CGContextRestoreGState(gc);
    
    CGGradientRelease(gradient);
    CGColorSpaceRelease(colorSpace);
    
    //注意释放CGMutablePathRef
    CGPathRelease(path);
    
    //从Context中获取图像，并显示在界面上
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
        
    return img;
        
}

@end
